/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.cardarus.operatingsystem.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;

/**
 * This class captures the details of a sales transaction.
 * @author Javier Snaider
 */
@Entity
public class SalesTransaction implements Serializable {
/**
 * The id of the sales transaction.
 */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    /**
     * The date of the sales transaction
     */
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date saleDate;
    /**
     * The Customer associated with this sale.
     */
    @ManyToOne
    private Customer customer;
    /**
     * The list of rows of this sale. Each row has
     * an item and quantity.
     */
    @OneToMany(mappedBy = "record", cascade=CascadeType.ALL)
    private List<SalesTransactionRow> rows;
    /**
     * The subTotal of the sale transaction. Taxes are not included.
     */
    private double subTotal;
    /**
     * The taxes of this sales transaction.
     */
    private double tax;
    /**
     * The total of this transaction. Total includes taxes.
     */
    private double total;
    /**
     * The number of points earned by Customer in this transaction.
     */
    private int pointsEarned;
    /**
     * The number of points that Customer has used in this transaction to
     * reduce the total.
     */
    private int pointsUsed;

    /**
     * The equivalent dollars for the points used
     */
    private double dollarsFromPoints;

    /**
     * the list of cetificates that Customer has used in this sales transaction.
     */
    

    @OneToOne(cascade=CascadeType.ALL)
    private Certificate certificate;
    /**
     * SystemUser that creates the sales transaction.
     */
    @ManyToOne
    private SystemUser systemUser;
    /**
     * Payment of this sales transaction.
     */
    @OneToOne(cascade=CascadeType.ALL)
    private Payment payment;

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the date
     */
    public Date getDate() {
        return saleDate;
    }

    /**
     * @param date the date to set
     */
    public void setDate(Date date) {
        this.saleDate = date;
    }
    
    /**
     * @return the customer
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    /**
     * @return the subTotal
     */
    public double getSubTotal() {
        return subTotal;
    }

    /**
     * @param subTotal the subTotal to set
     */
    public void setSubTotal(double subTotal) {
        this.subTotal = subTotal;
    }

    /**
     * @return the tax
     */
    public double getTax() {
        return tax;
    }

    /**
     * @param tax the tax to set
     */
    public void setTax(double tax) {
        this.tax = tax;
    }

    public double getNetSubTotal(){
        double net = subTotal - dollarsFromPoints;
        if (certificate != null){
            net = net - certificate.getDenomination();
        }
        return net;
    }
    /**
     * @return the total
     */
    public double getTotal() {
        return total;
    }

    /**
     * @param total the total to set
     */
    public void setTotal(double total) {
        this.total = total;
    }

    /**
     * @return the pointsEarned
     */
    public int getPointsEarned() {
        return pointsEarned;
    }

    /**
     * @param pointsEarned the pointsEarned to set
     */
    public void setPointsEarned(int pointsEarned) {
        this.pointsEarned = pointsEarned;
    }

    /**
     * @return the pointsUsed
     */
    public int getPointsUsed() {
        return pointsUsed;
    }

    /**
     * @param pointsUsed the pointsUsed to set
     */
    public void setPointsUsed(int pointsUsed) {
        this.pointsUsed = pointsUsed;
    }

    /**
     * @return the certificate
     */
    public Certificate getCertificate() {
        return certificate;
    }

    /**
     * @param certificate the certificate to set
     */
    public void setCertificates(Certificate certificate) {
        this.certificate = certificate;
    }

    /**
     * @return the user
     */
    public SystemUser getUser() {
        return getSystemUser();
    }

    /**
     * @param user the user to set
     */
    public void setUser(SystemUser user) {
        this.setSystemUser(user);
    }

    /**
     * @return the saleDate
     */
    public Date getSaleDate() {
        return saleDate;
    }

    /**
     * @param saleDate the saleDate to set
     */
    public void setSaleDate(Date saleDate) {
        this.saleDate = saleDate;
    }

    /**
     * @return the rows
     */
    public List<SalesTransactionRow> getRows() {
        return rows;
    }

    /**
     * @param rows the rows to set
     */
    public void setRows(List<SalesTransactionRow> rows) {
        this.rows = rows;
    }
    /**
     * adds a SalesTransactionRow in the list of rows.
     * @param row the SalesTransactionRow to add.
     */
    public void addRow(SalesTransactionRow row){
        getRows().add(row);
    }
    /**
     * @return the systemUser
     */
    public SystemUser getSystemUser() {
        return systemUser;
    }

    /**
     * @param systemUser the systemUser to set
     */
    public void setSystemUser(SystemUser systemUser) {
        this.systemUser = systemUser;
    }

    /**
     * @return the payment
     */
    public Payment getPayment() {
        return payment;
    }

    /**
     * @param payment the payment to set
     */
    public void setPayment(Payment payment) {
        this.payment = payment;
    }

    /**
     * @return the dollarsFromPoints
     */
    public double getDollarsFromPoints() {
        return dollarsFromPoints;
    }

    /**
     * @param dollarsFromPoints the dollarsFromPoints to set
     */
    public void setDollarsFromPoints(double dollarsFromPoints) {
        this.dollarsFromPoints = dollarsFromPoints;
    }
    
}
